package com.azxc.rapid.plus.system.controller;

import com.azxc.rapid.core.boot.ctrl.RapidController;
import com.azxc.rapid.core.excel.util.ExcelUtil;
import com.azxc.rapid.core.mp.support.Condition;
import com.azxc.rapid.core.mp.support.Query;
import com.azxc.rapid.core.tool.api.R;
import com.azxc.rapid.core.tool.constant.RapidConstant;
import com.azxc.rapid.core.tool.utils.DateUtil;
import com.azxc.rapid.core.tool.utils.Func;
import com.azxc.rapid.plus.system.entity.SystemInfoEntity;
import com.azxc.rapid.plus.system.excel.SystemInfoExcel;
import com.azxc.rapid.plus.system.excel.SystemInfoImporter;
import com.azxc.rapid.plus.system.service.ISystemInfoService;
import com.azxc.rapid.system.cache.RegionCache;
import com.azxc.rapid.system.entity.Region;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 系统配置信息 控制器
 */
@RestController
@AllArgsConstructor
@RequestMapping("/systemPlus/systemInfo")
@Api(value = "系统配置信息", tags = "系统配置信息")
public class SystemInfoController extends RapidController {

	private final ISystemInfoService systemInfoService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入systemInfo")
	public R<SystemInfoEntity> detail(SystemInfoEntity systemInfo) {
		SystemInfoEntity detail = systemInfoService.getOne(Condition.getQueryWrapper(systemInfo));
		return R.data(detail);
	}

	/**
	 * 分页 代码自定义代号
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入systemInfo")
	public R<IPage<SystemInfoEntity>> list(SystemInfoEntity systemInfo, Query query) {
		IPage<SystemInfoEntity> pages = systemInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(systemInfo));
		return R.data(pages);
	}

	/**
	 * 新增 代码自定义代号
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入systemInfo")
	public R save(@Valid @RequestBody SystemInfoEntity systemInfo) {
		return R.status(systemInfoService.save(systemInfo));
	}

	/**
	 * 修改 代码自定义代号
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入systemInfo")
	public R update(@Valid @RequestBody SystemInfoEntity systemInfo) {
		return R.status(systemInfoService.updateById(systemInfo));
	}


	/**
	 * 新增或修改 代码自定义代号
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入systemInfo")
	public R submit(@Valid @RequestBody SystemInfoEntity systemInfo) {
		String[] codes = StringUtils.split(systemInfo.getRegionCode(), ",");
		String residentAreaName = "";
		for (int i = 0; i < codes.length; i++) {
			Region region = RegionCache.getByCode(codes[i]);
			if (i == codes.length - 1) {
				residentAreaName = residentAreaName + region.getName();
			} else {
				residentAreaName = residentAreaName + region.getName() + "/";
			}
		}
		systemInfo.setRegionName(residentAreaName);
		return R.status(systemInfoService.saveOrUpdate(systemInfo));
	}

	/**
	 * 删除 代码自定义代号
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(systemInfoService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 修改启动状态
	 */
	@PostMapping("/triggerStatus")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "修改启动状态", notes = "修改启动状态")
	public R enabled(@RequestBody SystemInfoEntity systemInfo) {
		Boolean result = systemInfoService.update(systemInfo, new UpdateWrapper<SystemInfoEntity>().lambda().set(SystemInfoEntity::getEnabled, systemInfo.getEnabled()).eq(SystemInfoEntity::getId, systemInfo.getId()));
		return R.status(result);
	}

	/**
	 * 查询启动状态下的一行记录
	 */
	@GetMapping("/getEnabled")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "查询启动状态下的一行记录", notes = "查询启动状态下的一行记录")
	public R<List<SystemInfoEntity>> getEnabled(String modelType) {
		List<SystemInfoEntity> entity = systemInfoService.list(Wrappers.<SystemInfoEntity>lambdaQuery().eq(SystemInfoEntity::getModelType, modelType).eq(SystemInfoEntity::getEnabled, "1").orderByDesc(SystemInfoEntity::getCreateTime).last("limit 1"));
		return R.data(entity);
	}

	/**
	 * 导入
	 */
	@PostMapping("import")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "导入", notes = "传入excel")
	public R importSystemInfo(MultipartFile file, Integer isCovered) {
		SystemInfoImporter systemInfoImporter = new SystemInfoImporter(systemInfoService, isCovered == 1);
		ExcelUtil.save(file, systemInfoImporter, SystemInfoExcel.class);
		return R.success("操作成功");
	}

	/**
	 * 导出用户
	 */
	@GetMapping("export")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "导出", notes = "传入")
	public void exportSystemInfo(@ApiIgnore @RequestParam Map<String, Object> SystemInfo, HttpServletResponse response) {
		QueryWrapper<SystemInfoEntity> queryWrapper = Condition.getQueryWrapper(SystemInfo, SystemInfoEntity.class);
		queryWrapper.lambda().eq(SystemInfoEntity::getIsDeleted, RapidConstant.DB_NOT_DELETED);
		List<SystemInfoExcel> list = systemInfoService.exportSystemInfo(queryWrapper);
		ExcelUtil.export(response, "数据" + DateUtil.time(), "数据表", list, SystemInfoExcel.class);
	}

	/**
	 * 导出模板
	 */
	@GetMapping("template")
	@ApiOperationSupport(order = 14)
	@ApiOperation(value = "导出模板")
	public void exportUser(HttpServletResponse response) {
		List<SystemInfoExcel> list = new ArrayList<>();
		ExcelUtil.export(response, "数据模板", "数据表", list, SystemInfoExcel.class);
	}


}
